package com.share.two.bio;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
	
	@SuppressWarnings("resource")
	public static void main(String[] args) throws IOException {
		ServerSocket server = new ServerSocket(55555);
		System.out.println("服务器已经启动。。。。。。");
		
		while (true) {
			//线程会阻塞
			final Socket socket = server.accept();
			System.out.println("主线程");
			
			new Thread(new Runnable() {
				
				@Override
				public void run() {
					try {
						BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
						PrintStream ps = new PrintStream(socket.getOutputStream());
						
						//server接收
						String line = br.readLine();
						System.out.println("server接收：" + line);
						String back = new StringBuffer(line).reverse().toString();
						System.out.println("server返回：" + back);
						//server返回
						ps.println(back);
						
					} catch (IOException e) {
						e.printStackTrace();
					}
					
				}
			}).start();
		}
	}
}
